using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using FinancialAnalyzer.ToolClasses;

namespace FinancialAnalyzer.DataBase
{
    [Serializable]
    public class Year : IList<KeyValuePaar<Month, Double>>, IComparable<Year>
    {
        private List<KeyValuePaar<Month, Double>> _monthList;
        private String _nameOfYear;
        private Double _sumOfYear;
        private readonly Regex YEAR_REGEX = new Regex("\\d{4}");
        
        public Year(String s)
        {
            if (  YEAR_REGEX.Match(s).Success && s.Length == 4 )
            {
                _monthList = new List<KeyValuePaar<Month, Double>>();
                _nameOfYear = s;
                _sumOfYear = 0;
            }
            else    
            {
                  throw new FormatException("Unbekanntes Jahr, bitte halten sie sich an die Form JJJJ!");
            }

        }


        public String YearString
        {
            get { return _nameOfYear; }
            set { _nameOfYear = value;}
        }

        public Double SumOfYear
        {
            get { return _sumOfYear; }
            set { _sumOfYear = value; }
        }

        public void Add(Month month, Double val)
        {
            _monthList.Add(new KeyValuePaar<Month, Double>(month,val));
        }

        public Double Sum(Double valuevalency)
        {
            SumOfYear = 0;
            for (int i = 0; i < _monthList.Count; i++)
            {
                _monthList[i].Value = Math.Round(_monthList[i].Key.Sum(valuevalency),2);
                SumOfYear += _monthList[i].Value;
            }
            return SumOfYear;
        }

        public int IndexOf(String month)
        {
            for (int i = 0; i < _monthList.Count; i++)
            {
                if (_monthList[i].Key.ToString().Equals(month))
                {
                    return i;
                }
            }
            return -1;
        }

        public List<KeyValuePaar<String, Double>> Find(String searchString)
        {
            List<KeyValuePaar<String, Double>> tempList = new List<KeyValuePaar<String, Double>>();
            foreach (KeyValuePaar<Month, Double> paar in _monthList)
            {
                tempList.AddRange(paar.Key.Find(searchString));
            }
            return tempList;
        }

        #region overwritten Object Member
        public override bool Equals(object obj)
        {
            if (!(obj is Year)) 
            {
                return false;
            }
		
		    Year year = (Year) obj;
		    return _nameOfYear == year.YearString;
        }


        public bool Equals(String s)
        {
            return _nameOfYear.ToLower() == s.ToLower();
        }

        public override string ToString()
        {
            return YearString;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion

        #region IList<KeyValuePaar<Month,Double>> Member

        public int IndexOf(KeyValuePaar<Month, Double> item)
        {
            return _monthList.IndexOf(item);
        }

        public void Insert(int index, KeyValuePaar<Month, Double> item)
        {
            _monthList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _monthList.RemoveAt(index);
        }

        public KeyValuePaar<Month, Double> this[int index]
        {
            get
            {
                return _monthList[index];
            }
            set
            {
                _monthList[index] = value;
            }
        }

        #endregion

        #region ICollection<KeyValuePaar<Month,Double>> Member

        public void Add(KeyValuePaar<Month, Double> item)
        {
            _monthList.Add(item);
        }

        public void Clear()
        {
            _monthList.Clear();
        }

        public bool Contains(KeyValuePaar<Month, Double> item)
        {
            return _monthList.Contains(item);
        }

        public void CopyTo(KeyValuePaar<Month, Double>[] array, int arrayIndex)
        {
            _monthList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get {return  _monthList.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(KeyValuePaar<Month, Double> item)
        {
            return _monthList.Remove(item);
        }

        #endregion

        #region IEnumerable<KeyValuePaar<Month,Double>> Member

        public IEnumerator<KeyValuePaar<Month, Double>> GetEnumerator()
        {
            return _monthList.GetEnumerator();
        }

        #endregion

        #region IEnumerable Member

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _monthList.GetEnumerator();
        }

        #endregion

        #region IComparable<Year> Member

        public int CompareTo(Year other)
        {
            if (this.YearString.Equals(other.YearString))
            {
                return 0;
            }
            else
            {
                if (this.YearString.ToLower()[0] > other.YearString.ToLower()[0])
                {
                    return 1;
                }
                else
                {
                    return -1;
                }
            }
        }

        #endregion
    }
}
 